<?php

namespace app\admin\controller\admin;

use app\admin\controller\Controllers;
use app\admin\model\admin\Admin as AdminModel;
use app\admin\service\{
    Roles,Admin
};
use think\{
    Db,Exception,Request
};

class Administrator extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Admin $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\Admin.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);

        return view('admin@admin/administrator/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Roles $roles)
    {
        $roles = $roles->get([['status', '=', 1]]);
        $this->assign([
            'roles' => $roles
        ]);
        return $this->fetch('admin/administrator/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Admin $service)
    {
        $post = $this->request->only(['username','password','password_confirm','mobile','rid'], 'post');
        $this->validate($post, 'app\admin\validate\Admin.create');
        $adminId = $service->create($post);
        if ( false === $adminId ) {
            return failed('新增失败');
        }
        $service->role($adminId, $post['rid']);
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Admin $service, Roles $roles, int $id, AdminModel $admin)
    {
        $roleList = $service->getRole($id);
        $roles = $roles->get([['status', '=', 1]]);
        $this->assign([
            'roles' => $roles,
            'roleList' => $roleList,
            'adminInfo' => $admin
        ]);
        return view('admin@admin/administrator/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, Admin $service, int $id)
    {
        $put = $this->request->only(['password','password_confirm','mobile','rid'], 'put');
        $this->validate($put, 'app\admin\validate\Admin.update');
        $service->ultraVires($request->userInfo['type'], $id);
        Db::startTrans();
        if ( isset($put['password']) && !$put['password'] ) {
            unset($put['password']);
        }
        $adminUpdate = $service->save($id, $put);
        if ( !$adminUpdate ) {
            Db::rollback();
            return failed('更新失败');
        }
        $rid = get($put, 'rid', []);
        if ( count($rid) > 0 ) {
            try{
                $service->role($id, $rid);
            } catch ( Exception $exception ) {
                Db::rollback();
                return failed($exception->getMessage());
            }
        }
        Db::commit();
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Admin $service, Request $request, int $id)
    {
        if ( $id <= 0 ) {
            return failed('参数错误,刷新重试!');
        }
        $service->ultraVires($request->userInfo['type'], $id);
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-11-08
     * @return \think\Response
     */
    public function status(Admin $admin, AdminModel $adminModel)
    {
        return $admin->status($adminModel);
    }

    /**
     * 检测用户名是否唯一
     * @author zx
     * @date 2019-11-12
     * @return \think\Response
     */
    public function only(Admin $admin)
    {
        $get = $this->request->only(['id','username'], 'get');
        $this->validate($get, 'app\admin\validate\Admin.only');
        $bool = $admin->only($get);
        return json($bool);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-11-12
     * @return \think\Response
     */
    public function destory(Admin $admin)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\Admin.destory');
        if ( $admin->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 换肤
     * @author zx
     * @date 2020-01-09
     * @return \think\Response
     */
    public function skin(string $skin, Admin $service, Request $request)
    {
        $adminUpdate = $service->save($request->userInfo['id'], ['skin'=>$skin]);
        if ( !$adminUpdate ) {
            return failed('换肤失败');
        }
        return success();
    }

}
